package com.laivi.basic.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import com.laivi.basic.common.listener.CallBack;
import com.laivi.basic.module.log.Log;
import com.laivi.basic.module.log.Logs;

/**
 * @author laivi.zhu@gmail.com
 * @date 2014-6-19
 * description:
 */
public class FileUtil {
	private static final Log log = Logs.getLog(FileUtil.class);
	
	public static void readLineFile(String path,CallBack callback){
		FileUtil.readLineFile(path, "utf-8", callback);
	}
	
	public static void readLineFile(String path,String encoding,CallBack callback){
		BufferedReader br=null;
		try{
			if(DataUtil.isEmpty(encoding)){
				br=new BufferedReader(new InputStreamReader(new FileInputStream(new File(path))));
			}else{
				br=new BufferedReader(new InputStreamReader(new FileInputStream(new File(path)),encoding));
			}
			int i=0;
			String line=null;
			while((line=br.readLine())!=null){
				callback.execute(String.valueOf(i),line);
			}
		}catch(Exception e){
			log.error(e.getMessage());
		}finally{
			if(br!=null){
				try {
					br.close();
				} catch (IOException e) {
					log.error(e.getMessage());
				}
			}
		}
	}

}
